package com.example.petgrowing;


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.IOException;
import java.net.MalformedURLException;

public class MainActivity4 extends AppCompatActivity implements Runnable {
    private static final String TAG = "Net";
    Handler handler;
    TextView show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        show = findViewById(R.id.tv_res);

        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 7) {
                    String str = (String) msg.obj;
                    Log.i(TAG, "handleMessage:收到str=" + str);

                    show.setText(str);
                }
                super.handleMessage(msg);

            }
        };
    }

    public void onclick(View btn) {
        Log.i(TAG, "onclick:");
        Thread t = new Thread(this);
        Log.i(TAG, "onCreate:启动子线程");
        t.start();
    }
    public void gobackhome(View btn){
        Log.i(TAG,"gobackhome");
        finish();
    }
    public void run() {
        Log.i(TAG, "run:线程已运行");
        String retStr = "";
        try {
            Log.i(TAG, "run:正在工作。。。");
            Document doc = Jsoup.connect("https://hongloumeng.5000yan.com/").get();
            Log.i(TAG, "title=" + doc.title());
            Elements names = doc.getElementsByClass("paiban");
            for(Element e:names){
                for(int a = 0;a<120;a++){
                String title = (e.getElementsByClass("menu-item")).get(a).text();
                Log.i(TAG, "run:名字:" + title);
                retStr += (title +"\n");
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        Message msg = handler.obtainMessage();
        msg.what = 7;
        msg.obj = retStr;
        handler.sendMessage(msg);
    }
}